package conditionsAPI;

class NotCondition extends AndCondition{

	private Condition conditionToCancel;

	public NotCondition(Condition conditionToCancel) throws ConditionDuplicatedException{
		super("!"+conditionToCancel.getId());
		this.conditionToCancel = conditionToCancel;
		conditionToCancel.addConditionListener(this);
		this.onConditionStateUpdated(conditionToCancel);
	}

	private void conditionToCancelChanged() {
		this.state = !(this.conditionToCancel.isTrue());
		this.fireConditionChange(this);
	}

	private void preConditionChanged(Condition condition){
		this.updateConditionState(condition);
		if (this.arePreConditionsFulfilled()){
			this.conditionToCancel.setState(false);
			this.state = true;
			this.fireConditionChange(this);
		}
	}
	
	@Override
	public void onConditionStateUpdated(Condition condition) {
		if (this.isPreCondition(condition)){
			this.preConditionChanged(condition);
		}
		else if (this.conditionToCancel.equals(condition)){
			this.conditionToCancelChanged();
		}
	}
}
